package scanner.impl;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import exception.MyException;
import stringdispose.StringMatching;
import pojo.Customer;
import scanner.DataScanner;

public class DataScannerImpl implements DataScanner {
	private InputStream in;
	private Scanner scaner;
	
	public DataScannerImpl(InputStream in) {
		super();
		this.in = in;
		scaner=new Scanner(in);
		
	}

	public List<Customer> scann() throws IOException,MyException {
		List<String> list=preScan();
		List<Customer> list1=new ArrayList<Customer>();
		String reg="\\s+";
		for(String s:list){
			//System.out.println(s);
		    String[] strs = s.split(" ");
		    Customer c = new Customer();
		    if(StringMatching.intMatch(strs[0])){
		    	c.setId(Integer.parseInt(strs[0]));
		    }else{
		    	continue;
		    }
		    
		    if(StringMatching.nameMatch(strs[1])){
		    	c.setName(strs[1]);
		    }else{
		    	continue;
		    }
		    
		    if(StringMatching.emailMatch(strs[2])){
		    	c.setEmail(strs[2]);
		    }else{
		    	continue;
		    }
		   
		    
		    if(StringMatching.sexMatch(strs[3])){
		    	
		    	c.setSex(strs[3]);
		    }else{
		    	continue;
		    }
		    if(StringMatching.dateMatch(strs[4])){
		    	//System.out.println(strs[4]);
		    	c.setBirthday(strs[4]);
		    }else{
		    	continue;
		    }
		    
		    list1.add(c);
		}
		
		return list1;
	}
	private List<String> preScan() throws MyException {
		BufferedReader input = new BufferedReader(new InputStreamReader(in));
		List<String> dataList = new ArrayList<String>();
		String s = null;
		do{
			try {
				s = input.readLine() ;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				throw new MyException("","","");
			}
			if(s!=null)
				dataList.add(s);
		}while(s!=null);
		try {
			input.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			throw new MyException("","","");
		}
		return dataList;
	}
	
	
	

}
